<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

if( !isset( $_POST ) ) {
	print( "Don't! Just... don't!" );
	exit();
}
$army_id = getPostValue( 'army_id' );
$follow_id = getPostValue( 'follow_id' );

$query = "SELECT * " .
		"FROM army " .
		"WHERE army_id = '$army_id'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'ruler_id' ] ) && $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	$name = $row[ 'name' ];
	
	if( $follow_id == -1 ) {
		$query3 = "UPDATE army " .
				"SET follow = -1 " .
				"WHERE army_id = '$army_id'";
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$status = 'Follow army halted.';
	}
	else {
		if( $army_id == $follow_id ) {
			$status = 'Armies can\'t follow themselves.';
		}
		elseif( $row[ 'training' ] != 0 ) {
			$status = 'Can\'t follow an army while training.';
		}
		elseif( $row[ 'mission' ] != 0 ) {
			$status = 'Can\'t follow an army while on a mission.';
		}
		elseif( $row[ 'mission' ] != 0 ) {
			$status = 'Can\'t follow an army while moving.';
		}
		elseif( $row[ 'fortified' ] != 0 ) {
			$status = 'Can\'t follow an army while fortified.';
		}
		else {
			$query2 = "SELECT * " .
					"FROM army WHERE army_id = '$follow_id'";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			
			$query3 = "SELECT count(*) " .
					"FROM army_orders WHERE army_id = '$army_id'";
			$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
			
			$query4 = "SELECT count(*) " .
					"FROM army_orders WHERE army_id = '$follow_id'";
			$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
			$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
	
			if( !isset( $row2[ 'army_id' ] ) ) {
				$status = 'You can\'t follow non existent armies.';
			}
			elseif( $row2[ 'training' ] != 0 ) {
				$status = 'Can\'t follow an army which is training.';
			}
			elseif( $row2[ 'mission' ] != 0 ) {
				$status = 'Can\'t follow an army which is on a mission.';
			}
			elseif( $row2[ 'mission' ] != 0 ) {
				$status = 'Can\'t follow an army which is moving.';
			}
			elseif( $row2[ 'fortified' ] != 0 ) {
				$status = 'Can\'t follow an army which is fortified.';
			}
			elseif( $row2[ 'follow' ] != -1 ) {
				$status = 'Can\'t follow an army which is already following an army.';
			}
			elseif( $row[ 'actual_x' ] != $row2[ 'actual_x' ] || $row[ 'actual_y' ] != $row2[ 'actual_y' ] ) {
				$status = 'Can\'t follow an army which is not on the same location.';
			}
			elseif( $row3[ 'count(*)' ] > 0 ) {
				$status = 'Can\'t follow an army while still under orders.';
			}
			elseif( $row4[ 'count(*)' ] > 0 ) {
				$status = 'Can\'t follow an army which already has orders.';
			}
			else {
				$valid = false;
				$query3 = "SELECT faction_id " .
						"FROM faction_member " .
						"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( isset( $row3[ 'faction_id' ] ) ) {
					$query3 = "SELECT count(*) " .
							"FROM faction_member " .
							"WHERE ruler_id = " . $row2[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
					$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
					$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
					if( $row3[ 'count(*)' ] == 1 ) {
						$valid = true;
					}
				}
				else {
					if( $row2[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
						$valid = true;
					}
				}
				
				if( $valid ) {
					$query3 = "UPDATE army " .
							"SET follow = '$follow_id' " .
							"WHERE army_id = '$army_id'";
					mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
					$status = 'Follow army confirmed.';
				}
				else {
					$status = 'You can only follow allied or your own armies.';
				}
			}
		}
	}

	$_SESSION[ 'follow_order_status' ] = $status;
	header( "Location: army.php?name=$name" );	
}
else {
	header( "Location: armies.php" );
}
?>
